package com.chunfeng.lerunapplication.list;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.chunfeng.lerunapplication.R;
import com.chunfeng.lerunapplication.api.OrderApi;
import com.chunfeng.lerunapplication.dialog.UpdateOrderDialog;
import com.chunfeng.lerunapplication.dialog.VerifyDialog;
import com.chunfeng.lerunapplication.entity.JsonRequest;
import com.chunfeng.lerunapplication.entity.Orders;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;

/**
 * 订单项适配器
 *
 * @author by 春风能解释
 * <p>
 * 2023/12/4
 */
@Getter
public class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    /**
     * 订单API接口
     */
    private final OrderApi orderApi;
    /**
     * 订单描述
     */
    private TextView description;
    /**
     * 修改按钮
     */
    private Button updateButton;
    /**
     * 删除按钮
     */
    private Button deleteButton;
    /**
     * 当前数据
     */
    private Orders orders;
    /**
     * 应用上下文
     */
    private Context context;

    /**
     * 初始化组件
     *
     * @param itemView 界面
     */
    public ListHolder(@NonNull @NotNull View itemView) {
        super(itemView);
        context = itemView.getContext();
        description = itemView.findViewById(R.id.Description_TextView);
        updateButton = itemView.findViewById(R.id.Button_UpdateOrder);
        deleteButton = itemView.findViewById(R.id.Button_DeleteOrder);
        updateButton.setOnClickListener(this);
        deleteButton.setOnClickListener(this);
        orderApi = new OrderApi(context);
    }

    /**
     * 设置数据
     *
     * @param orders 订单
     */
    public void setOrders(Orders orders) {
        this.orders = orders;
    }

    /**
     * 初始化数据
     */
    private void initData() {
        description.setText(orders.getDescription());
    }

    /**
     * 点击事件
     *
     * @param v 点击的元素
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        //修改
        if (id == R.id.Button_UpdateOrder) {
            UpdateOrderDialog orderDialog = new UpdateOrderDialog(context)
                    .setTitle("修改订单")
                    .setDescription(orders.getDescription())
                    .setOnSubmitBtnClickListener(this::updateOrder)
                    .setOnCancelBtnClickListener(UpdateOrderDialog::dismiss);
            orderDialog.show();
        }
        //删除
        else if (id == R.id.Button_DeleteOrder) {
            VerifyDialog verifyDialog = new VerifyDialog(context)
                    .setTitle("警告")
                    .setMessage("你真的要删除这份订单吗?")
                    .setCancelButtonOnClickListener(VerifyDialog::dismiss)
                    .setOkButtonOnClickListener(this::deleteOneOrder);
            verifyDialog.show();
        }
    }

    /**
     * 删除一条订单
     *
     * @param dialog 弹窗
     */
    private void deleteOneOrder(VerifyDialog dialog) {
        JsonRequest<Integer> jsonRequest = orderApi.deleteOrders(new Integer[]{orders.getId()});
        if (!jsonRequest.getSuccess()) {
            Toast.makeText(context, jsonRequest.getMessage(), Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(context, "删除成功!", Toast.LENGTH_SHORT).show();
        dialog.dismiss();
    }

    /**
     * 修改订单
     *
     * @param updateOrderDialog 弹窗对象
     */
    private void updateOrder(UpdateOrderDialog updateOrderDialog) {
        String data = updateOrderDialog.descriptionView()
                .getText().toString();
        if (data.isEmpty()) {
            return;
        }
        orders.setDescription(data);
        JsonRequest<Integer> jsonRequest = orderApi.updateOneOrders(orders);
        if (!jsonRequest.getSuccess()) {
            Toast.makeText(context, jsonRequest.getMessage(), Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(context, "修改成功!", Toast.LENGTH_SHORT).show();
        initData();
        updateOrderDialog.dismiss();
    }
}
